package 蒙斯特;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = Integer.parseInt(in.nextLine());
        for (int cases = 1; cases <= T; cases++) {
            int n = Integer.parseInt(in.nextLine());
            String[] card = new String[n];
            for (int i = 0; i < n; i++) {
                card[i] = in.nextLine();
            }
            int res = 0;
            for (int i = 1; i < n; i++) {
                if (card[i].compareTo(card[i - 1]) < 0) {
                    res++;
                    for (int j = i; j >= 1; j--) {
                        if (card[j].compareTo(card[j - 1]) < 0) {
                            swap(card, j, j - 1);
                        }
                    }
                }
            }
            System.out.printf("Case #%d: %d\n", cases, res);
        }
    }

    private static void swap(String[] card, int x, int y) {
        String t = card[x];
        card[x] = card[y];
        card[y] = t;
    }
}
